var ns;
app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        form: {
            pageindex: 0,
            pagesize: 10,
            startTime: '',
            endTime: '',
            activeDate: 0,
            ChargeMoneyTypes: [],
            ChargeIDList: [],
            ProjectIDs: [],
            activeDateDesc: '时间',
            TotalCost: '',
            TotalBackCost: '',
            TotalCount:'',
            TotalBackCount:'',
            currentDayType: 2,
            activeRoomName: '请选择房间',
        },
        chargeMoneyTypeList: [],
        chargeSummaryList: [],
        projectList: [],
        list: [],
        can_scroll: false,
        scroll_top: 0,
        current_item_length: 0,
        shower: false,
        roomlist: [],
        roomId: 0,
        showsearchroom: false,
    },
    methods: {
        get_param: function () {
            var that = this;
            that.roomlist = [];
            if (!that.roomId) {
                that.get_data();
                return;
            }
            ns.post({
                action: 'getroomfeeaddparams',
                RoomID: that.roomId,
            }, function (succeed, data, err) {
                if (succeed) {
                    that.roomList = data.roomList;
                    if (that.roomList.length > 0) {
                        that.form.activeRoomName = '已选(' + that.roomList.length + ')个房间';
                        that.roomList.forEach(function (item) {
                            item.isSelected = true;
                        })
                    }
                }
                that.get_data();
            });
        },
        get_data: function () {
            var that = this;
            that.getSearchParams();
            setTimeout(function () {
                that.getAnalysisData();
            }, 1000);
            that.shower == true;
        },
        doSelectChargeMoneyType: function (ID) {
            var that = this;
            var index = that.form.ChargeMoneyTypes.indexOf(ID);
            if (index > -1) {
                that.form.ChargeMoneyTypes.splice(index, 1);
            } else {
                that.form.ChargeMoneyTypes.push(ID);
            }
        },
        doSelectChargeCategory: function (ID) {
            var that = this;
            var index = that.form.ChargeIDList.indexOf(ID);
            if (index > -1) {
                that.form.ChargeIDList.splice(index, 1);
            } else {
                that.form.ChargeIDList.push(ID);
            }
        },
        doSelectProject: function (ID) {
            var that = this;
            var index = that.form.ProjectIDs.indexOf(ID);
            if (index > -1) {
                that.form.ProjectIDs.splice(index, 1);
            } else {
                that.form.ProjectIDs.push(ID);
            }
        },
        getSearchParams: function () {
            var that = this;
            var options = {};
            options.action = "getanalysissearchparam";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.chargeMoneyTypeList = data.chargeMoneyTypeList;
                    that.chargeSummaryList = data.chargeSummaryList;
                    that.projectList = data.projectList;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        getAnalysisData: function () {
            var that = this;
            var options = {};
            if (that.roomId && that.roomList.length == 0) {
                return;
            }
            options.relateRoomId = that.roomId;
            if (that.roomId && that.roomList.length > 0) {
                var myRoomIdList = [];
                that.roomList.forEach(function (item) {
                    if (item.isSelected) {
                        myRoomIdList.push(item.id);
                    }
                })
                if (myRoomIdList.length == 0) {
                    api.toast({
                        msg: '请选择房间',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.list = [];
                    that.form.TotalCost = 0;
                    that.form.TotalBackCost = 0;
                    that.current_item_length = 0;
                    return;
                }
                options.relateRoomIds = JSON.stringify(myRoomIdList)
            }
            options.action = "getanalysisfeehistorydetaillist";
            options.ChargeIDList = JSON.stringify(that.form.ChargeIDList);
            options.ChargeMoneyTypes = JSON.stringify(that.form.ChargeMoneyTypes);
            options.ProjectIDs = JSON.stringify(that.form.ProjectIDs);
            options.StartChargeTime = that.form.startTime;
            options.EndChargeTime = that.form.endTime;
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.can_scroll = data.list.length == that.form.pagesize;
                    if (that.form.pageindex == 0) {
                        that.list = data.list;
                    } else {
                        that.list = that.list.concat(data.list);
                    }
                    that.form.TotalCost = data.TotalCost;
                    that.form.TotalBackCost = data.TotalBackCost;
                    that.form.TotalCount = data.TotalCount;
                    that.form.TotalBackCount = data.TotalBackCount;
                    that.current_item_length = that.list.length;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        doReset: function (type) {
            var that = this;
            if (type == 2) {
                that.form.startTime = '';
                that.form.endTime = '';
                that.form.activeDate = 0;
                that.form.activeDateDesc = '时间';
            } else if (type == 3) {
                that.form.activeRoomName = '请选择房间';
                that.roomList.forEach(function (item) {
                    item.isSelected = false;
                })
            } else {
                that.form.ChargeIDList = [];
                that.form.ChargeMoneyTypes = [];
                that.form.ProjectIDs = [];
            }
        },
        doSearch: function () {
            var that = this;
            that.doHideSearch();
            that.form.pageindex = 0;
            that.getAnalysisData();
        },
        doSelectRoom: function (item) {
            var that = this;
            item.isSelected = !item.isSelected;
            var nameList = [];
            that.roomList.forEach(function (item) {
                if (!item.isSelected) {
                    return false;
                }
                if (item.name && nameList.indexOf(item.name) == -1) {
                    nameList.push(item.name)
                }
            })
            that.form.activeRoomName = nameList.length > 0 ? '已选(' + nameList.length + ')个房间' : '请选择房间';
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
                that.showsearchroom = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchroom = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            if (type == 3) { //按关联房间
                that.showsearchparam = false;
                that.showsearchmonth = false;
                that.showsearchroom = !that.showsearchroom;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth || that.showsearchroom;
            if (type == 2 && that.showsearchmonth) {
                setTimeout(function () {
                    that.initPicker();
                }, 100);
            }
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearchroom = false;
            that.showsearch = false;
        },
        doClickDay: function (dataValue) {
            var that = this;
            that.form.activeDate = dataValue;
            var startDate = '',
                endDate = '';
            if (dataValue == 1) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate = dateRangeUtil.getDateString(currentDate);
                endDate = startDate;
                that.form.activeDateDesc = '今日';
            }
            if (dataValue == 2) {
                var currentMonth = dateRangeUtil.getCurrentMonth();
                startDate = dateRangeUtil.getDateString(currentMonth[0]);
                endDate = dateRangeUtil.getDateString(currentMonth[1]);
                that.form.activeDateDesc = '本月';
            }
            if (dataValue == 3) {
                var currentYear = dateRangeUtil.getCurrentYear();
                startDate = dateRangeUtil.getDateString(currentYear[0]);
                endDate = dateRangeUtil.getDateString(currentYear[1]);
                that.form.activeDateDesc = '本年';
            }
            that.form.startTime = startDate;
            that.form.endTime = endDate;
        },
        doOpenDetail: function (ID) {
            ns.openWin('analysis_historyfeedetail_frm', '收款单据详情', {
                ID: ID
            })
        },
        do_open_tongji: function () {
            ns.openWin('analysis_historyfeesummary_frm', '收支统计')
        },
        do_select_time: function (type) {
            var that = this;
            var currentTime = type == 1 ? that.form.startTime : that.form.endTime;
            api.openPicker({
                type: 'date',
                date: currentTime,
                title: '选择日期'
            }, function (ret, err) {
                if (ret) {
                    var year = ret.year;
                    var month = (ret.month >= 10 ? ret.month : '0' + ret.month);
                    var day = (ret.day >= 10 ? ret.day : '0' + ret.day);
                    currentTime = year + '-' + month + '-' + day;
                    if (type == 1) {
                        that.form.startTime = currentTime;
                    } else {
                        that.form.endTime = currentTime;
                    }
                }
            });
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //开始日期
                    var btn = document.querySelector('#startdate');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.startTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.startTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                    //结束日期
                    var btnEnd = document.querySelector('#enddate');
                    btnEnd.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.endTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.endTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                });
            })(mui, document);
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.currentDayType = api.pageParam.type || 2;
    app.roomId = api.pageParam.roomId || 0;
    app.doClickDay(app.form.currentDayType)
    setTimeout(function () {
        app.get_param();
    }, 500);
    // api.addEventListener({
    //     name: 'do_open_tongji'
    // }, function () {
    //     app.do_open_tongji();
    // });
};
var scroll = new auiScroll({
    listen: true,
    distance: 5 //判断到达底部的距离，isToBottom为true
}, function (ret) {
    if (ret.isToBottom && app.can_scroll) {
        if (app.scroll_top > ret.scrollTop) {
            app.scroll_top = ret.scrollTop;
            return;
        }
        app.scroll_top = ret.scrollTop + 1;
        app.form.pageindex = app.current_item_length;
        app.getAnalysisData();
    }
});
